package com.ws.utils.xmlUtil;

import java.util.*;

public class MapToXml {
	
	public static String mapToXML(Map map,String encoding) {  
		System.out.println("将Map转成Xml, Map：" + map.toString());
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\""+encoding+"\"?>");
		mapToXMLTest(map, sb , null);
		String xmlStr = sb.toString();
		xmlStr = xmlStr.replace("<nullTag>", "").replace("</nullTag>", "");
		System.out.println("将Map转成Xml, Xml：" + xmlStr);
		try {
			//return sb.toString().getBytes("UTF-8");
			return xmlStr;
		} catch (Exception e) {
		}
		return null;
	}

	private static void mapToXMLTest(Map map, StringBuffer sb,String hasObj) {
		Set set = map.keySet();
		for (Iterator it = set.iterator(); it.hasNext();) {
			String key = (String) it.next();
			Object value = map.get(key);
			if (null == value) value = "";
			if (value.getClass().getName().equals("java.util.ArrayList")) {
				ArrayList list = (ArrayList) value;
				String _obj = null;
				if(key.indexOf("_obj")>0){
					_obj=key.replace("_obj", "");
					for (int i = 0; i < list.size(); i++) {
						HashMap hm = (HashMap) list.get(i);
						mapToXMLTest(hm, sb , _obj);
					}
				}
				else{
					sb.append("<" + key + ">");
					for (int i = 0; i < list.size(); i++) {
						HashMap hm = (HashMap) list.get(i);
						mapToXMLTest(hm, sb , _obj);
					}
					sb.append("</" + key + ">");
				}
				
			} else {
				if(hasObj != null){
					sb.append("<" + hasObj + ">");
					if (value instanceof HashMap) {
						sb.append("<" + key + ">");
						mapToXMLTest((HashMap) value, sb , null);
						sb.append("</" + key + ">");  
					} else {
						sb.append("<" + key + ">" + value + "</" + key + ">");  
					}
					sb.append("</" + hasObj + ">");
				}else{
					if (value instanceof HashMap) {
						sb.append("<" + key + ">");
						mapToXMLTest((HashMap) value, sb , null);
						sb.append("</" + key + ">");  
					} else {
						sb.append("<" + key + ">" + value + "</" + key + ">");  
					}
				}
				
			}
		}
	}

    public static void main(String[] args) {  
        Map<String, Object> dataMap = new LinkedHashMap<String, Object>(); //默认  
        dataMap.put("rtnCode", "02");  
        dataMap.put("rtnMsg", "查询失败");  
        dataMap.put("idWltCloudDistrict", "专区id");  
          
        Map<String, Object> bizMap = new LinkedHashMap<String, Object>();  
        bizMap.put("head", "serviceId001");  
        bizMap.put("body", dataMap);//data节点是一个map
        
        Map<String, Object> rootMap = new LinkedHashMap<String, Object>();
        rootMap.put("root", bizMap);
        
        Map<String, Object> objMap = new LinkedHashMap<String, Object>();  
        objMap.put("idWltCloudDistrict", "专区id");  
        objMap.put("districtName", "专区名称");  
        objMap.put("validateDateStart", "专区有效期开始");  
        objMap.put("validateDateEnd", "专区有效期结束");  
        objMap.put("status", "专区状态(00-待发布;01-销售中; 02-已下架;)");  
        objMap.put("partnerId", "合作伙伴id");  
        objMap.put("channelId", "渠道id");  
        objMap.put("areaId", "商圈专区id");  
        dataMap.putAll(objMap); //  
        
        List<Map<String, Object>> prodKindList1 = new ArrayList<Map<String, Object>>();  
        for(int i=1;i<=2;i++){  
            // 第二层  
            Map<String, Object> prodKindList2 = new LinkedHashMap<String, Object>();
            List<Map<String, Object>> prodKindList4 = new ArrayList<Map<String, Object>>();  
            for(int j=1;j<=2;j++){  
                Map<String, Object> prodKindObj2 = new LinkedHashMap<String, Object>();  
                Map<String, Object> prodKindList = new LinkedHashMap<String, Object>();  
                prodKindObj2.put("idWltCloudProdKind", "类目ID");  
                prodKindObj2.put("prodKindName", "类目名称");  
                prodKindObj2.put("prodKindIdx", "类目序号");  
                Map<String, Object> prodKind2 = new LinkedHashMap<String, Object>();
                prodKind2.put("prodKind", prodKindObj2);
                prodKindList.put("prodKindList4", prodKind2);
                prodKindList4.add(prodKindList);
                //prodKindList2.add(prodKind2);  
            }
            prodKindList2.put("nullTag", prodKindList4);
            prodKindList2.put("count", 12);
              
            //第一层  
            Map<String, Object> prodKindOjb1 = new LinkedHashMap<String, Object>();  
            prodKindOjb1.put("idWltCloudProdKind", "类目ID");  
            prodKindOjb1.put("prodKindName", "类目名称");  
            prodKindOjb1.put("prodKindIdx", "类目序号");  
            prodKindOjb1.put("prodKindList", prodKindList2);  
            Map<String, Object> prodKind1 = new LinkedHashMap<String, Object>();  
            prodKind1.put("prodKind", prodKindOjb1);  
            prodKindList1.add(prodKind1);  
        }
          
        dataMap.put("prodKindList", prodKindList1);  
          
        mapToXML(rootMap,"UTF-8");  
    }
}